package com.xiaochong.bitong.job.component;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xc.logclient.utils.LogTrace;
import com.xiaochong.service.common.config.redis.RedisServer;
import com.xiaochong.tir.common.data.mapper.CurrencyInfoMapper;
import com.xiaochong.tir.common.data.po.CurrencyInfo;
import com.xiaochong.tir.common.util.CollectionUtil;
import com.xiaochong.tir.common.util.OssImageUtil;
import com.xiaochong.tir.common.util.RedisKeysUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 缓存初始化
 * Created by cx on 2018/5/4.
 */
@Component
public class RedisCacheComponent {

    @Autowired
    private CurrencyInfoMapper currencyInfoDao;
    @Autowired
    private RedisServer redisServer;
    @Autowired
    private OssImageUtil ossImageUtil;


    /**
     * 货币缓存 初始化
     */
    public List<CurrencyInfo> currencyInit(){
        List<CurrencyInfo> currencyInfoList = currencyInfoDao.selectByEntityList(new CurrencyInfo().setDeleteFlag(false).setEnableFlag(true));
        currencyInfoList.forEach(currencyInfo -> {
            currencyInfo.setImageUrl(ossImageUtil.fmtImg(currencyInfo.getImageUrl()));
            if(currencyInfo.getWeights()){
                redisServer.hashSet(RedisKeysUtils.CURRENCY_WEIGHTS_KEY,currencyInfo.getCmcId(),JSONObject.toJSONString(currencyInfo));
            }
            redisServer.hashSet(RedisKeysUtils.CURRENCY_INFO_KEY,currencyInfo.getCmcId(), JSONObject.toJSONString(currencyInfo));
        });
        return  currencyInfoList;
    }

    /**
     * 获取所有 货币信息
     * @return
     */
    public List<CurrencyInfo> getCurrencyList(){
        try{
            Map<String,String> currencyStringMap = redisServer.hashEntries(RedisKeysUtils.CURRENCY_INFO_KEY);;
            List<CurrencyInfo> currencyInfoList = new ArrayList<>();
            if(CollectionUtil.isNotBlank(currencyStringMap)){
                for(Map.Entry<String,String> map :currencyStringMap.entrySet()){
                    currencyInfoList.add(JSON.parseObject(map.getValue(),CurrencyInfo.class));
                }
                return currencyInfoList;
            }else{
                List<CurrencyInfo>  currencyList = currencyInit();
                return currencyList;
            }
        }catch (Exception e){
            LogTrace.error("redis连接异常",e);
            return null;
        }
    }
    /**
     * 获取所有 诊断货币信息
     * @return
     */
    public List<CurrencyInfo> getDiagnosisCurrencyList(){
        List<CurrencyInfo> currencyList = this.getCurrencyList();
        if(CollectionUtil.isBlank(currencyList)){
            return null;
        }
        List<CurrencyInfo> resultList = new ArrayList<>();
        for (CurrencyInfo currencyInfo:currencyList) {
            if(currencyInfo.getCurrencyDiagnosisFlag()!=null && currencyInfo.getCurrencyDiagnosisFlag()){
                resultList.add(currencyInfo);
            }
        }
        return resultList;
    }

    /**
     * 根据cmsId获取 货币信息
     * @param cmsId
     * @return
     */
    public CurrencyInfo getCurrencyInfo(String cmsId){
     String currencyInfo = redisServer.hashGet(RedisKeysUtils.CURRENCY_INFO_KEY,cmsId);
     if(StringUtils.isNotBlank(currencyInfo)){
         return JSONObject.parseObject(currencyInfo,CurrencyInfo.class);
     }
        currencyInit();
        return currencyInfoDao.selectByEntity(new CurrencyInfo().setCmcId(cmsId));

    }

}
